package com.cqcloud.platform.common.matter.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;

/**
 * @author weimeilayer@gmail.com ✨
 * @date 💓💕2025年6月25日🐬🐇 💓💕
 */
public class SpecificUtils {

	public static String getLocalIpAddress() {
		try {
			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
			while (interfaces.hasMoreElements()) {
				NetworkInterface iface = interfaces.nextElement();
				// 跳过回环和未启用的接口
				if (iface.isLoopback() || !iface.isUp())
					continue;

				// 遍历该接口的所有IP地址
				for (InetAddress addr : Collections.list(iface.getInetAddresses())) {
					// 检查是否是IPv4地址且是内网地址(192.168.x.x)
					if (!addr.isLoopbackAddress() && addr.getHostAddress().startsWith("192.168.")) {
						return addr.getHostAddress();
					}
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}